//////////////////////////////////////////////////
//              postGameHandler                 //
//////////////////////////////////////////////////
function getOneTimeCurrentStateOfGame() {
    var hr = new XMLHttpRequest();
    var url = "game_state.php";
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            parseOneTimeCurreuntStateOfGame(return_data);
        }
    };
    hr.send(null);
}
//////////////////////////////////////////////////
//   postGameHandler-Player Pressed BTN         //
//////////////////////////////////////////////////
function postGameHandler(command) {

    //fisrt Disable All Screen untill command ends
    SetBoardEnable(false);
    
    var hr = new XMLHttpRequest();
    var url = "game_handler.php";
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            parseHandlerResponse(return_data);
        }
    };
    var sendStr = "command="+command;
    hr.send(sendStr);
}
////////////////////////////////////////////////
//  postGameState                 //
////////////////////////////////////////////////
// called by the timer
function postGameState() {
    
    if ( IsGameEnded > 0 ) {
        return; //game ended, no need to parse move last time
    }
    
    var hr = new XMLHttpRequest();
    var url = "game_state.php";
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            
            var return_data = hr.responseText;  // get data
            // got answare!
            parseStateResponse(return_data);
        }
    };
    hr.send(null);
}
//////////////////////////////////////////////////
//              postGameSurrender               //
//////////////////////////////////////////////////
function postGameSurrender() {
    
    //fisrt Disable All Screen untill command ends
    SetBoardEnable(false);    
    
    var hr = new XMLHttpRequest();
    var url = "game_handler.php";
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;

            parseSurrender(return_data);
        }
    };
    var sendStr = "command="+COM_FORFIET;
    hr.send(sendStr);
}
////////////////////////////////////////////
//             getChatState               //
////////////////////////////////////////////
function getChatState() {
    var hr = new XMLHttpRequest();
    var url = "chat_state.php";
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;

            updateChat(return_data);
        }
    };
    hr.send(null);
}
//////////////////////////////////////////////
//              postChatLine                //
//////////////////////////////////////////////
function postChatLine(chatStr) {
    var hr = new XMLHttpRequest();
    var url = "chat_handler.php";
    hr.open("POST", url, true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;

            updateMyChat(return_data);
        }
    };
    var tmpstr = "chat="+chatStr;
    hr.send(tmpstr);
}
